<?php
class Sp{
    protected $main_table = 'data_sp';
	protected $main_id = 'sp_id';
	protected $db = null;
	
	public function __construct($db){
		$this->db=$db;
	}
   
	public function __destruct(){
		unset($this->db);
	}
   
	public function get_table_name(){
	    return $this->main_table;
	    
	}
	/**
	* 	insert news
	* 	@param array $data 
	* 	@return int affected count
	*/
	public function insert_data($data){
		if(!is_array($data))
		my_jump('输入的参数有误');
		$rows = $this->db->insert($this->main_table, $data);
		$insert_id = $this->db->getInsertId();
		return $insert_id;	
	}
	
	/**
	* 	update news
	* 	@param array $data 
	* 	@param string $where 
	* 	@return int affected count
	*/
	public function update_data($data,$where){
		if(!is_array($data))
		my_jump('输入的参数有误');
		$result = $this->db->update($this->main_table,$data,$where);
		return $result;
	}
	
	/**
	* 	delete news
	* 	@param string $where 
	* 	@return int affected count
	*/
	public function del_data($where){
		$result = $this->db->delete($this->main_table,$where);
		return $result;
	}
	
	/**
	* 	get news info
	* 	@param int $id 
	*	@param array/string $field
	* 	@return array one row data
	*/
	public function get_data_row($id,$field='*'){
		if(!intval($id))
		my_jump('输入的参数有误!');
		$sql = "SELECT ".$field." FROM ".$this->main_table." WHERE ".$this->main_id."='".intval($id)."' ";
		$data = $this->db->getRow($sql);
		return $data;
	}
	
	
	/**
	* 	get news array
	* 	@param string $where
	* 	@return array data
	*/
	public function get_data_array($where,$order='sp_id DESC',$limit='10',$field='*'){
		$sql = "SELECT ".$field." FROM ".$this->main_table." WHERE ".$where." ORDER BY ".$order;
		if(intval($limit)){
			$sql .= " LIMIT ".$limit;
		}
		$data = $this->db->getAll($sql);
		return $data;
	}   


	public function check_pwd($id,$password){
		$sql = "SELECT ".$this->main_id." FROM ".$this->main_table." WHERE ".$this->main_id."='".intval($id)."' AND sp_pwd='".base64_encode(md5($password))."' ";
		$result = $this->db->getOne($sql);
		if ($result) {
			return true;
		} else {
			return false;
		}
	}
	
	
	public function search_sp_by_title($sp_title){
		$sql = "SELECT ".$this->main_id." FROM ".$this->main_table." WHERE sp_title = '".$sp_title."' ";
		$result = $this->db->getOne($sql);
		if ($result) {
			return $result;
		} else {
			return false;
		}	
		
	}
	public function search_sp($sp_name){
		$sql = "SELECT ".$this->main_id." FROM ".$this->main_table." WHERE sp_name = '".$sp_name."' ";
		$result = $this->db->getOne($sql);
		if ($result) {
			return $result;
		} else {
			return false;
		}	
		
	}
	


	public function search_phone($phone){
		$sql = "SELECT ".$this->main_id." FROM ".$this->main_table." WHERE sp_phone = '".$phone."' ";
		$result = $this->db->getRow($sql);
		if ($result) {
			return $result;
		} else {
			return false;
		}	
		
	}

	public function del_sp($id){
		//刪除商家
		$result = $this->db->delete($this->main_table, "sp_id='".$id."'");
		//dump($row);
		if($result){
			return true;
		}else{
			return false;	
		}
		
	}


}
?>